package com.neusoft.utils.file;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.UUID;

import com.neusoft.utils.classLoader.ClassLoaderUtils;
import com.neusoft.utils.string.StringUtil;

/**
 * @ClassName: FileUtils
 * @Description: TODO 文件操作类
 * @author vocnhange
 * @date 2014-4-26 上午11:08:42
 */
public class FileUtils extends org.apache.commons.io.FileUtils {
	private static final String ENCODING = "UTF-8";
	private static final String SRCPATH = "src/main/java/";
	public static  String  URLSEPARATOR="/";
	/**
	 * @Title: getFilePath
	 * @Description: TODO 替换连接符 以便适应各个系统 比如lunix
	 * @param filePath
	 * @return
	 */
	public  static  String  getFilePath(String filePath){
		String result = StringUtil.replaceEach(filePath, new String[]{"/"}, new String[]{File.separator});
		return result;
	}
	
	
	@SuppressWarnings("unused")
	private static String getFilePath(String packageName, String name) {
		String path = packageName.replaceAll("\\.", "\\"+File.separator);
		return  path + File.separator + name;//"/" +
	}
	public static void deleteFolder(String path){
		try {
			deleteDirectory(new File(path));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	private static String getFileURLPath(String packageName, String name) {
		String path = packageName.replaceAll("\\.", URLSEPARATOR);
		return  path + URLSEPARATOR+ name;//"/" +path=URLDecoder.decode(path,ENCODING);
	}
	/**
	 *  TODO 获得类位置
	 * @param packageName 比如com.ht
	 * @param name
	 * @return
	 */
	public static File getClassPath(String packageName, String name) {	
		File file = null;
		try {
			file = ClassLoaderUtils.getResourceAsFile(getFileURLPath(packageName, name));
		} catch (IOException e) {
			e.printStackTrace();
		}
		return file;
	}

	public static String writeToString(File file) {
		String str = null;
		try {
			str = readFileToString(file, ENCODING);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return str;
	}
	private static String getSrcFilePath(String packageName, String name) {
		String path = packageName.replaceAll("\\.", "\\"+File.separator);
		return SRCPATH + path + File.separator + name;
	}
    public static File getSrcPath(String packageName, String name) {
		File file = new File(getSrcFilePath(packageName, name));
		return file;
	}
	/**
	 * @Title: filterOutFile
	 * @Description: TODO 过滤输出文件:如果没有路径文件夹 创建之
	 * @param outFile
	 * @return
	 */
	public static File newOutFile(File outFile) {
		
		return createFolder(outFile);
	}

	public static File newOutFile(String outfilePath) {
		return createFolder(new File(outfilePath));
	}

	public static File createFolder(File targetFile) {
		String path = targetFile.getAbsolutePath();
		String folder = StringUtil.substringBefore(path, targetFile.getName());
		File folderFile = new File(folder);
		if (!folderFile.exists()) {
			folderFile.mkdirs();
		}
		return targetFile;
	}


	/**
	 * @description 取指定文件的扩展名
	 * @param filePathName
	 *            文件路径
	 * @return 扩展名
	 */
	public static String getFileExt(String filePathName) {
		int pos = 0;
		pos = filePathName.lastIndexOf('.');
		if (pos != -1)
			return filePathName.substring(pos + 1, filePathName.length());
		else
			return "";
	}

	/**
	 * @description 读取文件大小
	 * @param filename
	 *            指定文件路径
	 * @return 文件大小
	 */
	public static int getFileSize(String filename) {
		try {
			File fl = new File(filename);
			int length = (int) fl.length();
			return length;
		} catch (Exception e) {
			return 0;
		}
	}

	/**
	 * @Description:生成新的文件名
	 * @param @param fileName
	 * @param @return
	 * @return String
	 * @throws BussinessException
	 * @see see_to_target
	 */
	public static String newFileName(String fileName) {
		String extName = null;
		// 获取扩展名
		if (fileName.lastIndexOf(".") >= 0) {
			extName = fileName.substring(fileName.lastIndexOf("."));
		}
		return StringUtil.remove(UUID.randomUUID().toString(), '-') + extName;// e
	}

	/**
	 * @Description:判断文件是否存在
	 * @param @param fileName 文件名
	 * @param @return
	 * @return boolean
	 * @throws BussinessException
	 * @see see_to_target
	 */
	public static boolean existFile(String fileName) {
		File file = new File(fileName);
		if (file.exists())
			return true;
		else
			return false;
	}
	public static void inputstreamToFile(InputStream ins,File file){
		   OutputStream os;
		try {
			os = new FileOutputStream(file);
			   int bytesRead = 0;
			   byte[] buffer = new byte[8192];
			   while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
			      os.write(buffer, 0, bytesRead);
			   }
			   os.close();
			   ins.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		}
	  public static void copyFileToDirectoryQ(File srcFile, File destDir){
		  try {
			    copyFileToDirectory(srcFile, destDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	  }

}
